home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / freeli20.zip / progs / hex2bin.asm < prev    next >
Assembly Source File  |  1996-07-01  |  2KB  |  89 lines

  1. Ideal
  2.  
  3. Public      main
  4. Extrn       startup:near
  5.  
  6. Macro       lcall p,a,b,c,d,e,f,g,h ;; library call
  7.  
  8.             ifnb <a>
  9.               push a                ;; if args, push first arg
  10.               lcall p,b,c,d,e,f,g,h ;; and recurse . . .
  11.             else
  12.               extrn p:near          ;; declare procedure
  13.               call p                ;; call procedure
  14.             endif
  15.  
  16. EndM
  17.  
  18. Model Tiny
  19. CodeSeg
  20. P186
  21. Org 100h
  22.  
  23. Start:      jmp startup
  24.  
  25. ;****************** Strings Section
  26.  
  27. Syntax      db 'Syntax:  HEX2BIN <infile> <outfile>',0
  28.  
  29. ;****************** 'main' procedure
  30.  
  31. Proc        main
  32.  
  33.             lcall fsetbuf 16384     ;Set file buffers to 16K
  34.  
  35.             cmp cx,2                ;Wrong number of args?
  36.             jne m_syntax
  37.  
  38.             lcall fopen [di],0      ;Open input file
  39.             test ax,ax              ;File not found?
  40.             jz m_syntax
  41.             xchg bp,ax              ;BP = handle
  42.  
  43.             lcall fopen [di+2],3    ;Open output file
  44.             test ax,ax              ;Check for errors
  45.             jnz m_ok1
  46.             lcall fclose bp         ;Close input file
  47.             jmp m_syntax            ;Go print syntax
  48.  
  49. m_ok1:      xchg di,ax              ;DI = handle
  50.             mov dx,0100h            ;DX = bit-buffer
  51.  
  52. m_loop:     lcall fgetc bp          ;Get char
  53.             test ax,ax              ;EOF, done
  54.             jl m_done
  55.             cmp al,'0'              ;Digit?
  56.             jl m_loop
  57.             cmp al,'9'
  58.             jle m_digit
  59.             cmp al,'A'              ;Uppercase letter?
  60.             jl m_loop
  61.             cmp al,'F'
  62.             jle m_letter
  63.             cmp al,'a'              ;Lowercase letter?
  64.             jl m_loop
  65.             cmp al,'f'
  66.             jg m_loop
  67.  
  68. m_letter:   sub al,7                ;Convert to binary
  69. m_digit:    and al,15
  70.             shl dl,4                ;Shift in digit
  71.             or dl,al
  72.             neg dh                  ;Flip flag
  73.             jl m_loop               ;Nonzero, loop
  74.  
  75.             lcall fputc di,dx       ;Output char
  76.             jmp m_loop              ;Loop back
  77.  
  78. m_done:     lcall fclose bp         ;Close files
  79.             lcall fclose di
  80.             ret                     ;Return
  81.  
  82. m_syntax:   push offset(Syntax)     ;Display 'Syntax' message
  83.             lcall puts
  84.             ret                     ;Return
  85.  
  86. EndP        main
  87.  
  88. End Start
  89.